package com.yunkaweilai.android.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.web.WebActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.base.BaseFragment;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.CountConsumeBean;
import com.yunkaweilai.android.model.CountIncomeBean;
import com.yunkaweilai.android.model.CountMainBean;
import com.yunkaweilai.android.model.CountRechargeBean;
import com.yunkaweilai.android.model.CountTopLIstBean;
import com.yunkaweilai.android.model.home.AppHomeModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.home.CountFifthView;
import com.yunkaweilai.android.view.home.CountFooterView;
import com.yunkaweilai.android.view.home.CountForthView;
import com.yunkaweilai.android.view.home.CountNoTopListView;
import com.yunkaweilai.android.view.home.CountSecondView;
import com.yunkaweilai.android.view.home.CountThirdView;
import com.yunkaweilai.android.view.home.CountTopListHead;
import com.yunkaweilai.android.view.home.HeaderShopInfoView;
import com.yunkaweilai.android.view.home.SmoothListView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 统计fragment
 */
public class CountFragment extends BaseFragment implements BGARefreshLayout.BGARefreshLayoutDelegate{

    @BindView(R.id.content_view)
    BGARefreshLayout contentView;
    @BindView(R.id.id_smoothListView)
    SmoothListView idSmoothListView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultiplestatusview;

    private CountMainBean countBean;
    private boolean isfirst = true;

    private CommonAdapter<CountTopLIstBean.DataBean.Goods> adapter;
    private HeaderShopInfoView headerShopInfoView; // 统计头列表
    private CountSecondView mCountSecondView;//第二个布局
    private CountThirdView mCountThirdView;//第三个布局  店铺收入
    private CountForthView mCountForthView;//第四个布局  会员充值
    private CountTopListHead mCountTopListHead;//热销产品顶部布局
    private CountFifthView mCountFifthView;//第五个布局  消费金额
    private CountFooterView mCountFooterView;//底部布局
    private CountNoTopListView mCountNoTopListView;//无热销产品布局
    private TitleBuilder titleBuilder;
    private ArrayList<CountTopLIstBean.DataBean.Goods> list = new ArrayList<>();


    @Override
    public int getContentViewId() {
        return R.layout.fragment_count;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        titleBuilder = new TitleBuilder(rootView);
        titleBuilder.setTitleText("统计");
        initRefreshLayout(contentView, this, false);
        return rootView;
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        contentView.beginRefreshing();
        initHttp();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initHttp();
        initAdapter();
    }

    private void initAdapter() {
        adapter = new CommonAdapter<CountTopLIstBean.DataBean.Goods>(mAc, R.layout.item_list_operation_home, list) {
            @Override
            protected void convert(final ViewHolder viewHolder, final CountTopLIstBean.DataBean.Goods item, int position) {

                viewHolder.setText(R.id.id_tv_head, position+1+"");
                switch (position){
                    case 0:
                        viewHolder.setBackgroundRes(R.id.id_tv_head,R.drawable.bg_item_top_list_head);
                        break;
                    case 1:
                        viewHolder.setBackgroundRes(R.id.id_tv_head,R.drawable.bg_item_top_list_head1);
                        break;
                    case 2:
                        viewHolder.setBackgroundRes(R.id.id_tv_head,R.drawable.bg_item_top_list_head2);
                        break;
                    default:
                        viewHolder.setBackgroundRes(R.id.id_tv_head,R.drawable.bg_item_top_list_head3);
                        break;
                }
                viewHolder.setText(R.id.id_tv_name, item.getGoods_name() + "");
                viewHolder.setText(R.id.id_tv_count, item.getGoods_num() + item.getUnit());
            }
        };
    }

    private void initHttp() {
        //请求上半部分
        HttpRequest.postUrl(Api.COUNT_HOME)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        if (contentView != null) {
                            contentView.endRefreshing();
                        }
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            countBean = gson.fromJson(json, CountMainBean.class);
                            if (isfirst) {
                                addView();
                            } else {
                                refreshView();
                            }
                            if (idMultiplestatusview != null) {
                                idMultiplestatusview.showContent();
                            }
                        } else {
                            if (idMultiplestatusview != null) {
                                idMultiplestatusview.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (contentView != null) {
                            contentView.endRefreshing();
                        }
                        if (idMultiplestatusview != null) {
                            idMultiplestatusview.showError();
                        }
                    }
                });

        HttpRequest.postUrl(Api.COUNT_TOPLIST)
                .addParams("type","sale")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        if (contentView != null) {
                            contentView.endRefreshing();
                            boolean checkCode = Utils.checkCode(mAc, json);
                            if (checkCode) {
                                Gson gson = new Gson();
                                CountTopLIstBean lIstBean =new CountTopLIstBean();
                                lIstBean = gson.fromJson(json, CountTopLIstBean.class);
                                list.clear();
                                list.addAll(lIstBean.getData().getList());
                                if (isfirst) {
                                } else {
                                    refreshView();
                                }
                                if (idMultiplestatusview != null) {
                                    idMultiplestatusview.showContent();
                                }else {
                                    if (idMultiplestatusview != null) {
                                        idMultiplestatusview.showError();
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultiplestatusview != null) {
                            idMultiplestatusview.showError();
                        }
                    }
                });
    }


    public void refresh() {
        contentView.beginRefreshing();
        initHttp();
    }

    private void addView() {
        headerShopInfoView.fillView(countBean.getData().getInfo(),idSmoothListView);
        mCountSecondView.fillView(countBean.getData().getInfo(),idSmoothListView);
        mCountThirdView.fillView(new CountIncomeBean.DataBean(),idSmoothListView);
        mCountForthView.fillView(new CountRechargeBean.DataBean(),idSmoothListView);
        mCountFifthView.fillView(new CountConsumeBean.DataBean(),idSmoothListView);
        mCountTopListHead.fillView(new CountIncomeBean.DataBean(),idSmoothListView);
        mCountFooterView.fillView(new CountIncomeBean.DataBean(),idSmoothListView);
        if (list.isEmpty()){
            mCountNoTopListView.fillView(new CountIncomeBean.DataBean(),idSmoothListView);
        }
        idSmoothListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        isfirst = false;
    }

    //刷新布局
    private void refreshView() {
        headerShopInfoView.refresh(countBean.getData().getInfo());
        mCountSecondView.refresh(countBean.getData().getInfo());
        mCountThirdView.refresh();
        mCountForthView.refresh();
        mCountFifthView.refresh();
        mCountNoTopListView.refresh(list,idSmoothListView);
    }

    private void initView() {
        mCountSecondView = new CountSecondView(mAc);
        headerShopInfoView = new HeaderShopInfoView(mAc);
        mCountThirdView = new CountThirdView(mAc);
        mCountForthView = new CountForthView(mAc);
        mCountFifthView = new CountFifthView(mAc);
        mCountTopListHead = new CountTopListHead(mAc);
        mCountFooterView = new CountFooterView(mAc);
        mCountNoTopListView = new CountNoTopListView(mAc);
    }
}
